﻿using IMES.Common.Domain.KeepModel.ProductionFile;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Keep.API.Appliction.ProductionPlanFile
{
	/// <summary>
	/// 获取生产计划列表
	/// </summary>
	public class ProductionPlanListHandler:IRequestHandler<ProductionPlanListCommon, ResultPage<ProductionPlan>>
	{
		private readonly IBaseRepositorys<ProductionPlan> _productionPlanRep;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="productionPlanRep"></param>
		public ProductionPlanListHandler(IBaseRepositorys<ProductionPlan> productionPlanRep)
		{
			_productionPlanRep = productionPlanRep;
		}

		public Task<ResultPage<ProductionPlan>> Handle(ProductionPlanListCommon request, CancellationToken cancellationToken)
		{
			var result = new ResultPage<ProductionPlan>() { Code = 0, Messege = "获取生产计划列表成功" };
			try
			{
				var productionPlan = _productionPlanRep.GetAll();
				if (!string.IsNullOrEmpty(request.DocumentNumber))
				{
					productionPlan = productionPlan.Where(c => c.DocumentNumber == request.DocumentNumber);
				}
				if (productionPlan == null)
				{
					result.Code = (int)CodeEnum.search;
					result.Messege = "暂无数据";
					return Task.FromResult(result);
				}

				result.totalCount = productionPlan.Count();
				result.totalPage = (int)Math.Ceiling(result.totalCount / (double)request.PageSize);  
                result.Data = productionPlan.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
				return Task.FromResult(result);	 
			}
			catch (Exception ex)
			{
				result.Code = (int)CodeEnum.error;
				result.Messege = ex.Message;
			}
			return Task.FromResult(result);
			
		}
	}
}
